package com.fyzk.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.prize.item.PrizeItemBase;
import com.fyzk.activity.prize.item.PrizeItemFactory;
import com.fyzk.bean.PrizeHistoryItem;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.quickAdapter.easyRegularAdapter;
import com.tyjc.lottery.R;

import java.util.ArrayList;

public class PrizeHistoryAdapter extends easyRegularAdapter<PrizeHistoryItem, PrizeHistoryAdapter
        .ViewHolder> {

    @Override
    protected void withBindHolder(ViewHolder holder, final PrizeHistoryItem data, int position) {
        PrizeHistoryItem item = this.mArr_historyItems.get(position);
        PrizeItemBase itemBase = PrizeItemFactory.getInstance().getPrizeItem(item);
        itemBase.initNumView(((ViewHolder) holder).mLl_num);
        (holder).mTxt_peroid.setText(mContext.getString(R.string.text_di_qi, (item.phase)));
        (holder).mTxt_date.setText(item.drawTime);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(data);
                }
            }
        });

    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(PrizeHistoryItem data);
    }

    @Override
    protected int getItemDataPosFromInternalPos(int touch_position) {
        return super.getItemDataPosFromInternalPos(touch_position);
    }


    @Override
    protected int getNormalLayoutResId() {
        return R.layout.layout_prizehistory_item;
    }

    @Override
    protected ViewHolder newViewHolder(View view) {
        return new ViewHolder(view, true);
    }


    @Override
    public ViewHolder newFooterHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder newHeaderHolder(View view) {
        return new ViewHolder(view, false);
    }

    private ArrayList<PrizeHistoryItem> mArr_historyItems;
    private Context mContext;

    public PrizeHistoryAdapter(Context context, ArrayList<PrizeHistoryItem> arr_historyitem) {
        super(arr_historyitem);
        this.mContext = context;
        this.mArr_historyItems = arr_historyitem;
    }

    public void setOnDragStartListener(OnStartDragListener dragStartListener) {
        mDragStartListener = dragStartListener;
    }

    @Override
    public int getAdapterItemCount() {
        return mArr_historyItems.size();
    }

    class ViewHolder extends UltimateRecyclerviewViewHolder {

        public TextView mTxt_peroid;
        public TextView mTxt_date;
        public LinearLayout mLl_num;
        public View itemView;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                this.itemView = itemView;
                itemView.setBackgroundColor(Color.WHITE);
                mTxt_peroid = (TextView) itemView.findViewById(R.id.prizehistory_txt_peroid);
                mTxt_date = (TextView) itemView.findViewById(R.id.prizehistory_txt_date);
                mLl_num = (LinearLayout) itemView.findViewById(R.id.prizehistory_txt_number);
            }
        }

        @Override
        public void onItemSelected() {
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemClear() {
            itemView.setBackgroundColor(0);
        }

    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        swapPositions(fromPosition, toPosition);
//        notifyItemMoved(fromPosition, toPosition);
        super.onItemMove(fromPosition, toPosition);
    }

    @Override
    public void onItemDismiss(int position) {
        if (position > 0)
            removeAt(position);
        // notifyItemRemoved(position);
        //        notifyDataSetChanged();
        super.onItemDismiss(position);
    }
}